package cn.flying.base.process.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import cn.flying.base.process.bo.ProcException;
import cn.flying.base.process.model.CfProcException;

/**
 * @description: 流程异常信息
 * @author: lvyong
 * @date: 2023年02月23日 10:37
 * @version: 1.0
 */
@Mapper
public interface ProcessExceptionMapper {

    /**
     * 保存
     *
     * @param procException
     */
    @Insert("insert into cf_proc_exception (id, proc_inst_id, execution_id, proc_def_id, proc_def_key, proc_def_name, activiti_id, activiti_name, biz_key, exception_msg, exception_trace, deal_state, tenant_id, revision, created_by, created_time, updated_by, updated_time, del_flag" +
            "values (#{id}, #{procInstId}, #{executionId}, #{procDefId}, #{procDefKey}, #{procDefName}, #{activitiId}, #{activitiName}, #{bizKey}, #{exceptionMsg}, #{exceptionTrace}, #{dealState}, #{tenantId}, #{revision}, #{createdBy}, " +
            "createdTime}, #{updatedBy}, #{updatedTime}, #{delFlag})")
    void insert(CfProcException procException);


    /**
     * 查询所有的异常
     *
     * @param bizKey
     * @return
     */
    @Select("<script> " +
            "select t.* from ( " +
            "select a.id_ as id, " +
            "b.act_id_ as activiti_id, " +
            "e.node_name as activiti_name, " +
            "d.business_key_ as biz_key, " +
            "a.duedate_ as create_time, " +
            "a.exception_msg_ as exception_msg, " +
            "a.execution_id_ as execution_id, " +
            "a.proc_def_id_ as proc_def_id, " +
            "c.name_ as proc_def_name, " +
            "b.proc_inst_id_ as proc_inst_id, " +
            "b.tenant_id_ as tenant_id  " +
            "from " +
            "act_ru_deadletter_job a, " +
            "act_ru_execution b, " +
            "act_re_procdef c, " +
            "act_ru_execution d  " +
            "cf_proc_node e  " +
            "where " +
            "a.execution_id_ = b.id_  " +
            "and b.proc_def_id_ = c.id_  " +
            "and a.process_instance_id_ = d.id_ " +
            "and c.id_ = e.proc_def_id " +
            "and b.act_id_ = e.node_id" +
            "<if test='bizKey != null and bizKey != \"\"'> and d.business_key_ = #{bizKey} </if>" +
            ") t" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "bizKey", column = "biz_key"),
            @Result(property = "activitiId", column = "activiti_id"),
            @Result(property = "activitiName", column = "activiti_name"),
            @Result(property = "executionId", column = "execution_id"),
            @Result(property = "procDefId", column = "proc_def_id"),
            @Result(property = "procDefName", column = "proc_def_name"),
            @Result(property = "procInstId", column = "proc_inst_id"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "exceptionMsg", column = "exception_msg"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    List<ProcException> getAllException(String bizKey);
}




